<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row :gutter="20">
              <!-- 搜索框区域 -->
                <el-col :span="8">
                     <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
                        <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
                    </el-input>
                </el-col>
               <!-- 添加用户区域 -->
                <el-col :span="4">
                    <el-button type="primary" @click="dialogFormVisible = true">添加用户</el-button>
                </el-col>
            </el-row>
            <!-- 添加用户弹出框内容 -->
             <el-dialog title="添加用户" :visible.sync="dialogFormVisible" width="50%" @close="addDialogClosed">
                <el-form label-width="70px" :model="addForm" ref="addFormRef" :rules="addFormRules">
                  <el-form-item label="用户名" prop="username">
                    <el-input v-model="addForm.username"></el-input>
                  </el-form-item>
                  <el-form-item label="密码" prop="password">
                    <el-input v-model="addForm.password"></el-input>
                  </el-form-item>
                  <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email"></el-input>
                  </el-form-item>
                  <el-form-item label="手机" prop="mobile">
                    <el-input v-model="addForm.mobile"></el-input>
                  </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addUser">确 定</el-button>
                </div>
            </el-dialog>
            <user-table :list="UserList" @NewUserList="NewUserOP"></user-table>
            <user-paging :pagenum="queryInfo.pagenum" :pagesize="queryInfo.pagesize" :total="total" @NewSize="SetNewsize" @NewPage="SetNewpage"></user-paging>
        </el-card>
    </div>
</template>
<script>
import userTable from './children/table'
import userPaging from './children/paging'
export default {
  components: {
    userTable,
    userPaging
  },
  data () {
    // 验证邮箱的规则
    const checkEmail = (rule, value, callback) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
      if (regEmail.test(value) || value === '') {
        return callback()
      }
      callback(new Error('请输入合法的邮箱'))
    }
    // 验证手机的规则
    const checkMobile = (rule, value, callback) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value) || value === '') {
        return callback()
      }

      callback(new Error('请输入合法的手机号'))
    }
    return {
      // 添加用户框状态
      dialogFormVisible: false,
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
      total: 0,
      UserList: [],
      // 添加用户的表单数据
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 添加表单效验规则
      addFormRules: {
        username: [
          { required: true, message: '用户名不能为空！', trigger: 'blur' },
          { min: 3, max: 10, message: '用户名的长度在3~10个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '用户名的长度在6~15个字符之间',
            trigger: 'blur'
          }
        ],
        email: [
          { validator: checkEmail, trigger: 'blur' }
        ],
        mobile: [
          { validator: checkMobile, trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    // 重新渲染
    NewUserOP () {
      this.getUserList()
    },
    // 添加用户
    addUser () {
      // validate 对整个表单进行校验
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('users', this.addForm)
        if (res.meta.status !== 201) {
          this.$message.error(res.meta.msg)
          return
        }
        this.$message.success(res.meta.msg)
        // 隐藏添加用户的对话框
        this.dialogFormVisible = false
        // 重新获取数据
        this.getUserList()
      })
    },
    // 关闭的回调
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    // 根据从paging.vue中获取的新的页码  和  当前页数重新请求和渲染数据
    SetNewsize (newSize) {
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    SetNewpage (NewPage) {
      this.queryInfo.pagenum = NewPage
      this.getUserList()
    },

    // 获取用户列表数据
    async getUserList () {
      const { data: res } = await this.$http.get('users', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error(this.meta.msg)
      this.UserList = res.data.users
      this.total = res.data.total
    }
  }
}
</script>
